﻿using AutoMapper;
using ImoviesBlazorAntPro.Server.Repository;
using ImoviesBlazorAntPro.Shared.Dtos;
using ImoviesBlazorAntPro.Shared.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ImoviesBlazorAntPro.Server.Controllers
{
    /// <summary>
    /// 动漫播放链接控制器
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class AnimationVlinkController : ControllerBase
    {
        private readonly IAnimationVlinkRepository _animationVlinkRepository;
        private readonly IMapper _mapper;

        public AnimationVlinkController(IAnimationVlinkRepository animationRepository, IMapper mapper)
        {
            this._animationVlinkRepository = animationRepository;
            this._mapper = mapper;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="animationVlink"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> Add([FromBody] AnimationVlinkForCreateDto animationVlink)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            var avl = _mapper.Map<AnimationVlink>(animationVlink);
            _animationVlinkRepository.AddAnimationVlink(avl);
            bool res = await _animationVlinkRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="animationVlink"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult> Update([FromBody] AnimationVlinkForUpdateDto animationVlink)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            var avl = _mapper.Map<AnimationVlink>(animationVlink);
            _animationVlinkRepository.UpdateAnimationVlink(avl);
            bool res = await _animationVlinkRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ActionResult> Delete([FromRoute] uint id)
        {
            await _animationVlinkRepository.DeleteAnimationVlink(id);
            bool res = await _animationVlinkRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("batchDetele")]
        public async Task<ActionResult> BatchDetele([FromBody] List<uint> ids)
        {
            _animationVlinkRepository.DeleteBatch(ids);
            bool res = await _animationVlinkRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }
    }
}
